package com.ghost.manager
{
	import com.ghost.data_struct.HashMap;
	import com.ghost.model.Global_data;
	import com.ghost.model.Global_ui_data;
	import com.ghost.model.Object_pool;
	
	import flash.events.Event;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	
	import flashx.textLayout.elements.GlobalSettings;
	
	/**
	 * @author ghost
	 * 声音管理
	 */	
	public class Sound_manager
	{
		private static var instance:Sound_manager;
		public static function getInstance():Sound_manager
		{
			if(instance == null)
			{
				instance = new Sound_manager();
			}
			return instance;
		}
		public var playing_mp3_music_v:Vector.<String>;//当前正在播放的音乐mp3url
		//public var playing_mp3_effect_v:Vector.<String>;//当前正在播放的音效mp3url
		public var default_loop:int = 1000;//音乐默认播放次数
		public function Sound_manager()
		{
			playing_mp3_music_v = new Vector.<String>();
			//playing_mp3_effect_v = new Vector.<String>();
		}
		/**
		 * @param url 声音路径
		 * @param type 0音乐 1音效
		 * @param loop 循环次数默认1000
		 */		
		public function play_mp3(url:String,type:int=0,loop:int=1000):void
		{
			if(!Object_pool.sound_obj_pool.containsKey(url))
			{
				var sound:Sound = new Sound();
				sound.load(new URLRequest(url));
				sound.addEventListener(Event.COMPLETE,sound_Handler);
				var channel:SoundChannel = sound.play(0,loop);
				Object_pool.sound_obj_pool.put(url,{sound1:sound,channel1:channel,type1:type});
				channel.addEventListener(Event.SOUND_COMPLETE,function():void{play_endHandler.call(null,url)});
			}
			else
			{
				Object(Object_pool.sound_obj_pool.get(url)).sound1.play(0,loop);
			}
			playing_mp3_music_v.push(url);
			vol_init(Object_pool.sound_obj_pool.get(url));
			if(!Global_data.music_bool)Object(Object_pool.sound_obj_pool.get(url)).channel1.stop();
		}
		//播放结束
		private function play_endHandler(url:String):void
		{
			var n:int = playing_mp3_music_v.indexOf(url);
			if(n!=-1)
			{
				if(Object(Object_pool.sound_obj_pool.get(url)).type1==1)
				{
					playing_mp3_music_v.splice(n,1);
				}
				else
				{
					Object(Object_pool.sound_obj_pool.get(url)).sound1.play(0,default_loop);//音乐继续播放
				}
			}
		}
		//加载完成
		private function sound_Handler(e:Event):void
		{
			Sound(e.target).removeEventListener(Event.COMPLETE,sound_Handler);
			trace('sound_loaded');
		}
		/**
		 * 停止播放此mp3
		 * @param url mp3路径
		 */		
		public function stop_mp3(url:String):void
		{
			var n:int = playing_mp3_music_v.indexOf(url);
			if(n!=-1)
			{
				playing_mp3_music_v.splice(n,1);
				Object(Object_pool.sound_obj_pool.get(url)).channel1.stop();
			}
		}
		/**
		 *关闭音乐播放功能 
		 */		
		public function stop_all_mp3():void
		{
			var n:int = playing_mp3_music_v.length;
			var i:int = 0;
			for(;i<n;i++)
			{
				Object(Object_pool.sound_obj_pool.get(playing_mp3_music_v[i])).channel1.stop();
			}
			Global_data.music_bool = false;
		}
		/**
		 *开启播放音乐功能 
		 */		
		public function play_all_mp3():void
		{
			var n:int = playing_mp3_music_v.length;
			var i:int = 0;
			for(;i<n;i++)
			{
				Object(Object_pool.sound_obj_pool.get(playing_mp3_music_v[i])).sound1.play();
			}
			Global_data.music_bool = true;
		}
		/**
		 *改变正在播放的音乐的音量 
		 */		
		public function change_vol():void
		{
			var n:int = playing_mp3_music_v.length;
			var i:int = 0;
			for(;i<n;i++)
			{
				vol_init(Object_pool.sound_obj_pool.get(playing_mp3_music_v[i]));
			}
		}
		//改变音量
		private function vol_init(sound_map:Object):void
		{
			var channel:SoundTransform;
			channel = SoundChannel(sound_map.channel1).soundTransform;
			channel.volume = Global_data.music_vol;
			SoundChannel(sound_map.channel1).soundTransform = channel;
		}
	}
}